import os
from pyglet import image
from pyglet.media import Player
from pyglet.sprite import Sprite
from math import cos
from math import sin
from math import atan
from math import degrees
from math import pi
from math import fabs

#from theGame import *
from sprite import *
from arrow import *
from common import *

class Indian(GameSprite):
	
	def __init__(self, **kwargs):
		GameSprite.__init__(self, "player.png", **kwargs)
		# dammage is only melee dammage. the indian can only deal dammage with his arrows
		self.dammage = 0
		self.jump_force = 200
		self.safe_timer = 0
		self.base_speed = 100
		self.max_speed = 150
		self.name = "player"
		self.health = 10
		# bird jump limitation
		self.jump_counter = 0
		# walk sound
		self.sound.queue(load_music("walk.ogg", streaming=False))
		# run sound player
		self.run_sound = Player()
		self.run_sound.eos_action = Player.EOS_LOOP
		self.run_sound.queue(load_music("run.ogg", streaming=False))
		self.sound_copy = self.sound
		# bow
		self.bow = Sprite(load_image("bow.png"), self.x, self.y)
	
	def update(self, dt):
		self.safe_timer -= dt
		
		if (is_bear() and self.health < 10):
			self.health += 7 * dt
		
		# should the player run fast?
		if (is_buffalo()):
			self.speed = self.max_speed
			self.sound = self.run_sound
			if not (self.sound == self.run_sound):
				self.sound.pause()
				self.sound = self.run_sound
				if (self.right or self.left):
					self.play_sound()
		else:
			self.speed = self.base_speed
			if not (self.sound == self.sound_copy):
				self.sound.pause()
				self.sound = self.sound_copy
				if (self.right or self.left):
					self.play_sound()
		
		GameSprite.update(self, dt)
		
		self.bow.x = self.x + 25
		self.bow.y = self.y + 15
		
		if (self.touch_ground() and (self.left or self.right)):
			self.play_sound()
		else:
			self.stop_sound()
		
		# touches an other sprite?
		if (self.safe_timer <= 0):
			for other in Game.sprites:
				if (not other.name in ("player", "arrow", "cactus", "spawn")
					and self.intersect(other)
					and other.health > 0):
					if (other.name == "goal"):
						Game.victory = True
						self.speed = 0
						self.health = 1e99
					self.health -= other.dammage
					self.safe_timer = 1
					self.force_jump()
	
	def draw(self, offset):
		GameSprite.draw(self, offset)
		self.bow.x -= offset
		self.bow.draw()
	
	def jump(self):
		# only birds can jump while in air
		if self.touch_ground():
			self.jump_counter = 0
			self.y += 1
			self.vertical += self.jump_force
		if (is_bird() and 30 * self.jump_counter < self.jump_force):
			self.vertical += self.jump_force - 30 * self.jump_counter
			self.jump_counter += 1
			if (self.vertical > 200):
				self.vertical = 200
	
	def hit_wall(self, x):
		"""Called whenever sprite bumps against
		a wall"""
		# block path
		if (x == int(self.x / 10)):
			self.x = int(self.x / 10) * 10 + 10
		else:
			self.x = int((self.x + self.image.width) / 10) * 10 - self.image.width
	
	def aim(self, offset, aim_x, aim_y):
		self.bow.rotation = 25 - degrees(atan((aim_y - self.y) / (aim_x - (self.x - offset) + 0.01)))
		if (aim_x + offset < self.x):
			self.bow.rotation += 180
		
	
	def shoot(self, offset, aim_x, aim_y):
		if (not self.delete):
			sound = load_music("shoot.ogg", streaming=False).play()
			angle = atan((aim_y - self.y) / (aim_x - (self.x - offset) + 0.01))
			power = 200 + (5 * len(Game.feathers))
			dammage = 1 + (0.1 * len(Game.feathers))
			#print("Hp", self.health, "dammage", dammage)
			#print("bird", Game.bird, "buffalo", Game.buffalo, "bear", Game.bear)
			if (aim_x > self.x - offset):
				angle = atan((aim_y - self.y) / (aim_x - (self.x - offset)))
			else:
				angle = atan((aim_y - self.y) / (aim_x - (self.x - offset))) + pi
			
			Game.sprites.append(Arrow(angle, power,
			dammage, x = self.x + 25, y = self.y + 15))
			if (is_buffalo()):
				for delta in range(-2, 3):
					Game.sprites.append(Arrow(angle + (delta / 10.00),
					power + abs(delta) * 20, dammage, x = self.x + 25,
					y = self.y + 15))
	
	def die(self):
		GameSprite.die(self)
		Game.game_over = True

	